package com.aaa.dem1027.第二遍.d02;

import java.lang.reflect.Field;

/**
 * @Author: whj
 * @Version 1.0
 * @Date: 2022/10/27  15:06
 */
public class Test {
    public static void main(String[] args) throws Exception {
        Class<Teacher> teacherClass = Teacher.class;

        //1.获取本类中指定的属性对象
        Field name = teacherClass.getDeclaredField("name");
        System.out.println("name = " + name);
        Field age = teacherClass.getDeclaredField("age");
        System.out.println("age = " + age);

        //2 获取所有的属性
        Field[] declaredFields = teacherClass.getDeclaredFields();
        for (Field field : declaredFields) {
            System.out.println("field = " + field);
        }

        //3.获取本类以及父类中指定的public属性
        Field name1 = teacherClass.getField("name");
        System.out.println("name1 = " + name1);
        Field address = teacherClass.getField("address");
        System.out.println("address = " + address);

        //4.获取本类以及父类中的所有的public属性
        Field[] fields = teacherClass.getFields();
        for (Field field : fields) {
            System.out.println("field = " + field);
        }
    }


}

class People{
    public String address;
    private String sex;
}

class Teacher extends People{
    public String name;
    private Integer age;

    @Override
    public String toString() {
        return "Teacher{" +
                "sex='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}